package edu.uwosh.cs342.project4;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import edu.uwosh.cs342.project4.model.Place;

public class myItemizedOverlay extends ItemizedOverlay<OverlayItem> {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	Context mContext;

	public myItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	public myItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}

	public void addOverlay(OverlayItem overlay) {
		if (mOverlays.size() != 0) {
			mOverlays.remove(0);
		}
		mOverlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	@Override
	protected boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
		CharSequence text = item.getTitle() + " " + item.getSnippet();
		int duration = Toast.LENGTH_LONG;

		Toast toast = Toast.makeText(mContext, text, duration);
		toast.show();
		return true;
	}


	public void addNearby(ArrayList<Place> placesArray) {
		for (Place place : placesArray) {
			double lati, longi;
			lati = Double.parseDouble(place.getLat());
			longi = Double.parseDouble(place.getLng());
			GeoPoint p = new GeoPoint((int) (lati * 1E6), (int) (longi * 1E6));
			String info = place.getName();
			OverlayItem newOverlayitem = new OverlayItem(p, info, "lat: "
					+ lati + " long: " + longi);
			mOverlays.add(newOverlayitem);
		}
		populate();
	}
}
